package com.example.permission.myjbm.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.LoginActivity;
import com.example.jbmsuppliers.sinopec.activity.MyRegistrMessage;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.DoubleButtonDialog;
import com.example.permission.myjbm.activitys.Release_Commodity_1;
import com.example.permission.myjbm.net.ReleaseNet;
import com.jbm.jbmsupplier.util.BusinessEntity;


/**
 * 发布页面
 */
public class ReleasePopup {
    private  static ReleasePopup popup;
    private final PopupWindow window;
    private final Intent intent,intent1;
    private final ReleaseNet net;
    private final DialogLoading loader;
    private LoginSharedPreferences sp;
    private Context c;

    public static ReleasePopup getInstance(Context c){
        synchronized (ReleasePopup.class) {
            if (popup==null) {
                popup=new ReleasePopup(c);
            }
            return popup;
        }

    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ReleaseNet.MERCHANTERROR:
                    //检测是否是商家的接口
                    BusinessEntity info=(BusinessEntity)msg.obj;
                    if(info.back){
                        if(info.getStatus()==1){
                            //通过了  是商家
                            sp.saveMerchantState(true);
                            c.startActivity(intent);
                            window.dismiss();
                        }else if(info.getStatus()==0){
                            sp.saveMerchantState(false);
                            hint("等待审核中");
                        }else if(info.getStatus()==2){
                            sp.saveMerchantState(false);
                            c.startActivity(intent1);
                            window.dismiss();

                        }else{
                            //不是商家
                            sp.saveMerchantState(false);
                            c.startActivity(intent1);
                            window.dismiss();

                        }
                    }else{
                        sp.saveMerchantState(false);
                        c.startActivity(intent1);
                        window.dismiss();
                     //   hint("请注册商家");
                    }
                    break;

            }
            loader.dismiss();
        }
    };



    private ReleasePopup(final Context c){
        this.c=c;
        net=new ReleaseNet(handler);
         sp=new LoginSharedPreferences(c);
        loader=new DialogLoading(c);
        final View popupView = View.inflate(c, R.layout.dsy_aty_release, null);
         window=new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAsset.getScreenHeight(c));
        window.setAnimationStyle(R.style.myPopup);
          intent= new Intent(c, Release_Commodity_1.class);
        intent1= new Intent(c, MyRegistrMessage.class);
        GridView gv = (GridView) popupView.findViewById(R.id.release_gv_type);
        ImageView release_close = (ImageView) popupView.findViewById(R.id.release_close);
        release_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });

        gv.setAdapter(new MyTypeAdater(c));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position){
            case 0:
                intent.putExtra("category",1);
                break;
            case 1:
                intent.putExtra("category",2);
                break;
            case 2:
                intent.putExtra("category",4);
                break;
            case 3:
                intent.putExtra("category",6);
                break;
        }
        //检测是否登陆
        if(!sp.getLoginState()){
            c.startActivity(new Intent(c, LoginActivity.class));
            window.dismiss();
            return ;
       }
        //点击的第一个下表  检测是否是商家
        if(position==0){
            if(sp.getMerchantState()==false){
                loader.show();
                net.getMerchant();
                return ;
            }
        }
            c.startActivity(intent);
            window.dismiss();

    }
});

    }


    //提示
    public    void hint(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(
                c, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();

            }
        });
        dialog.setisVisibility();

    }


    public void show(View view){
        window.showAtLocation(view, Gravity.BOTTOM,-1,-1);
    }

class MyTypeAdater extends BaseAdapter{
    private Context c;
    private int photo[]={R.mipmap.guanggao,R.mipmap.gonggong,R.mipmap.huzhu,R.mipmap.quwei};
public MyTypeAdater(Context c){
        this.c=c;
}
    @Override
    public int getCount() {
        return photo.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView=View.inflate(c,R.layout.dsy_item_type,null);
        ImageView img = (ImageView) convertView.findViewById(R.id.type_img);
        img.setImageResource(photo[position]);
        return convertView;
    }
}

}
